<?php
class Auth extends MY_Controller{
	//获取token
	function get_token(){
		$data = $this->user_post_data;
		// var_dump($data);
		if(!isset($data['user_token']['api_secret'])){
			echo json_encode(array("code" => "0", "msg" => "缺少密钥"));
			exit;
		}
		$user_info = get_field_by_where("users", "id, api_secret", "id='{$data['user_token']['user_id']}'");
		if(!$user_info){
			echo json_encode(array("code" => "0", "msg" => "用户不存在"));
			echo exit;
		}
		if($user_info['api_secret'] != $data['user_token']['api_secret']){
			json_encode(array("code" => 0, "msg" => "错误的密钥"));
			exit;
		}
		$token_info = get_field_by_where("user_tokens", "id", "user_id='{$data['user_token']['user_id']}'");
		$token_data['token'] = create_rand_str(32, 32);
		$token_data['token_expiration'] = $this->token_expiration;
		$token_data['create_time'] = time();
		$token_data['user_id'] = $data['user_token']['user_id'];
		$token_data['token_expiration_date'] = date("Y-m-d H:i:s", $token_data['create_time']+$token_data['token_expiration']);
		if($token_info){
			$this->db->update("user_tokens", $token_data, array("id='{$token_info['id']}'"));
		}else{
			$this->db->insert("user_tokens", $token_data);
		}
		echo json_encode(array("code" => 1, "token_info" => $token_data));
	}
}